package com.whut.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javassist.bytecode.Descriptor.Iterator;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.hibernate.mapping.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.whut.model.User;
import com.whut.service.UserManager;
import com.whut.service.impl.UserManagerImpl;
import com.whut.util.UtilCommon;
import com.whut.vo.UserRegisterInfo;

public class UserAction extends ActionSupport implements ModelDriven {

	private UserManager um;
	private List<User> users;
	private User user;
	private String result;
	private UserRegisterInfo userInfo = new UserRegisterInfo();

	private HashMap<String, Object> jMap; 
	private int page;
	private int rows;
	
	public UserAction() {  
		//初始化Map对象  
		jMap = new HashMap<String, Object>();  
    } 

	//注册
	@Override
	public String execute() {
		jMap.clear();
		// TODO Auto-generated method stub
		System.out.println(userInfo.getUsername());
		System.out.println(userInfo.getPassword());
		System.out.println(userInfo.getEmail());
		User u = new User();
		u.setUsername(userInfo.getUsername());
		u.setPassword(UtilCommon.md5Encryption(userInfo.getPassword()));
		u.setEmail(userInfo.getEmail());
		u.setCompany(userInfo.getCompany());
		u.setDepartment(userInfo.getDepartment());
		u.setPermission(userInfo.getPermission());
		u.setGender(userInfo.getGender());
		u.setPhone(userInfo.getPhone());
		u.setRealName(userInfo.getRealName());
		u.setWorkingPlace(userInfo.getWorkingPlace());
			 	 
		try {
			if(um.exists(u)){
				jMap.put("success",false); 
				System.out.println("用户已存在");
				jMap.put("msg","用户已存在"); 
			} else {
				//在数据库添加新的用户
				um.add(u);		
				// 通过json返回成功
				jMap.put("success",true); 				
			} 
			UtilCommon.writeJson(jMap); 
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jMap.put("success",false); 
			jMap.put("msg","异常");
			UtilCommon.writeJson(jMap); 
			return null;
		}
	}
  
	//dataGrid 
	public String dataGrid() throws Exception {
		try {
			jMap.clear();
			System.out.println(page);
			System.out.println(rows);
			users=um.getUsersByPage(page, rows);
        	System.out.println("正常取出列表");
        	jMap.put("total",um.getTotal(new User()));
        	jMap.put("rows", users);
        	System.out.println(jMap);       	
        	UtilCommon.writeJson(jMap); 
        	return null;  // writeJson已经respone了，这里返回null即可
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("取用户列表发生异常");
			System.out.println(e);
			return "error";
		}		
		
	}  
	
	// 更新用户信息 
	public String editUser() {
		jMap.clear();
		// TODO Auto-generated method stub
		System.out.println(userInfo.getUsername());
		System.out.println(userInfo.getPassword());
		System.out.println(userInfo.getEmail());
		User u = new User();
		u.setId(userInfo.getId());
		u.setUsername(userInfo.getUsername());
		u.setPassword(userInfo.getPassword());
		u.setEmail(userInfo.getEmail());
		u.setCompany(userInfo.getCompany());
		u.setDepartment(userInfo.getDepartment());
		u.setPermission(userInfo.getPermission());
		u.setGender(userInfo.getGender());
		u.setPhone(userInfo.getPhone());
		u.setRealName(userInfo.getRealName());
		u.setWorkingPlace(userInfo.getWorkingPlace());
			 	 
		try {
			 
			//在数据库更新用户
			um.editUser(u);		
			// 通过json返回成功
			jMap.put("success",true);
//			users = um.findById(u.getId());
//			jMap.put("object", users.get(0));
			 
			UtilCommon.writeJson(jMap); 
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jMap.put("success",false); 
			jMap.put("msg","异常");
			UtilCommon.writeJson(jMap); 
			return null;
		}
	}
	
	// 更新用户信息 
	public String removeUser() {
		jMap.clear();		
		User u = new User();
		u.setId(userInfo.getId());
					 	 
		try {
			 
			//在数据库更新用户
			um.removeUser(u);		
			// 通过json返回成功
			jMap.put("success",true);
//			users = um.findById(u.getId());
//			jMap.put("object", users.get(0));
			 
			UtilCommon.writeJson(jMap); 
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jMap.put("success",false); 
			jMap.put("msg","异常");
			UtilCommon.writeJson(jMap); 
			return null;
		}
	}

	public UserManager getUm() {
		return um;
	}

	//@Resource(name = "userManager")
	public void setUm(UserManager um) {
		this.um = um;
	}

	// @Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return userInfo;
	}

	public String list() throws Exception {
		this.users = this.um.getUsers();
		return "list";
	}
		
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	

	public UserRegisterInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserRegisterInfo userInfo) {
		this.userInfo = userInfo;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public HashMap<String, Object> getjMap() {
		return jMap;
	}


	public void setjMap(HashMap<String, Object> jMap) {
		this.jMap = jMap;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

}
